/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.niclun.projectwebshop.nbp.Entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

/**
 *
 * @author niclas
 */
@Entity
public class Movie implements Serializable, Comparable {
    private static final long serialVersionUID = 1L;
    @Id
    @NotNull
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long movieId;

    @NotNull
    @Column(name="Title")
    private String title;
    @NotNull
    @Column(name="Description")
    private String description;
    @NotNull
    @Column(name="Director")
    private String director;
    
    @ManyToMany(cascade=CascadeType.ALL)
    List<Actor> actors;
    
    private String movieCover;
    
    private String trailerUrl;
    @NotNull
    @Column(name="ProductionYear")
    private int prodYear;
    
    @Column(name="Rating")
    private double rating;
    
    @Temporal(value= TemporalType.DATE)
    @Column(name="AddedToDatabase")
    private Date addedToDb;
    
    @NotNull
    private int price;

    public static enum Genre {
        Action,
        Comedy,
        Drama,
        Thriller,
        Sport,
        Crime,
        Adventure,
        Romance,
        History,
        Horror,
        Fantasy,
        Family,
        SciFi
    }
    
    @Enumerated(EnumType.STRING)
    private List<Genre> genres;

    public Movie() {}
    
    public Movie(String title, String director) {
        this.title = title;
        this.director = director;
        genres= new ArrayList<Genre>();
        actors = new ArrayList<Actor>();
    }
    
    public Long getMovieId() {
        return movieId;
    }
    
    public void setMovieId(Long id) {
        this.movieId = id;
    }
    
    public Date getAddedToDb() {
        return addedToDb;
    }

    public void setAddedToDb(Date addedToDb) {
        this.addedToDb = addedToDb;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public int getProdYear() {
        return prodYear;
    }

    public void setProdYear(int prodYear) {
        this.prodYear = prodYear;
    }

    public double getRating() {
        return rating;
    }

    public void setRating(double rating) {
        this.rating = rating;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTrailerUrl() {
        return trailerUrl;
    }

    public void setTrailerUrl(String trailerUrl) {
        this.trailerUrl = trailerUrl;
    }

    public String getMovieCover() {
        return movieCover;
    }

    public void setMovieCover(String movieCover) {
        this.movieCover = movieCover;
    }
    
    public List<Actor> getActors() {
        return actors;
    }

    public void setActors(List<Actor> actors) {
        this.actors = actors;
    }
    
    public void addActor(Actor actor) {
        actors.add(actor);
    }
    
    public void removeActor(Actor actor) {
        Iterator<Actor> it = actors.iterator();
        Actor a;
        while(it.hasNext()) {
            a = it.next();
            if(a.getActorId().equals(actor.getActorId()))
                it.remove();
        }
    }
    
    public void setGenres(List<Genre> genres) {
        this.genres = genres;
    }
    
    public List<Genre> getGenres() {
        return this.genres;
    }
    
    public void addGenre(Genre genre) {
        this.genres.add(genre);
    }
    
    public void removeGenre(Genre genre) {
        genres.remove(genre);
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
    public String getGenreAsString(){
        String str = "";
        for(int i=0; i < genres.size(); i++){
            str+= genres.get(i).toString();
            if(i < genres.size()-1)
                str+= ", ";
        }
        return str;
    }

    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (movieId != null ? movieId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Movie)) {
            return false;
        }
        Movie other = (Movie) object;
        if ((this.movieId == null && other.movieId != null) || (this.movieId != null && !this.movieId.equals(other.movieId))) {
            return false;
        }
        return true;
    }
    
    public int compareDate(Movie m) {
        Date compareDate = m.getAddedToDb();
        return addedToDb.compareTo(compareDate);
    }
    
    @Override
    public int compareTo(Object o) {
        if(o != null && o instanceof Movie) {
            Movie m = (Movie)o;
            return getMovieId().compareTo(m.getMovieId());
        }
        return -1;
    }

    @Override
    public String toString() {
        return "edu.chl.niclun.projectwebshop.nbp.Movie[ id=" + movieId + " ]";
    }
}
